//
//  HLCommonViewModel.swift
//  HLXiMaLaYa
//
//  Created by hulinhu on 2018/2/27.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import NSObject_Rx
import RxDataSources
import ReusableKit
import Then
import RxGesture

public enum HCCommonViewModelType {
    
    case none                             // 未知
    case mine                             // 我的
    case setting                          // 设置
}

class HLCommonViewModel: NSObject {

    private let vmDatas = Variable<[[HLCommonCellModel]]>([])
}

extension HLCommonViewModel : HLViewModelType {
    
    typealias Input = HLCommonCellInput
    typealias Output = HLCommonCellOutput
    
    struct HLCommonCellInput {
        
        let type: HCCommonViewModelType
        init(type: HCCommonViewModelType) {
            self.type = type
        }
    }
    
    struct HLCommonCellOutput {
        
        let sections: Driver<[HCCommonCellSection]>
        init(sections: Driver<[HCCommonCellSection]>) {
            self.sections = sections
        }
    }
    
    func transform(input: HLCommonViewModel.HLCommonCellInput) -> HLCommonViewModel.HLCommonCellOutput {
        
        let temp_sections = vmDatas.asObservable().map({ (sections) -> [HCCommonCellSection] in
            return sections.map({ (models) -> HCCommonCellSection in
                return HCCommonCellSection(items: models)
            })
        }).asDriver(onErrorJustReturn: [])
        
        let output = HLCommonCellOutput(sections: temp_sections)
        
        let sectionArr: [[HLCommonCellModel]]
        
        switch input.type {
        case .none:
            sectionArr = []
            break
        case .setting:
            sectionArr = HLMineCellFactory.loadSettingModels()
            break
        case .mine:
            sectionArr = HLMineCellFactory.loadMineModels()
            break
        }
        self.vmDatas.value = sectionArr
        
        return output
    }

}

struct HCCommonCellSection {
    
    var items: [Item]
}

extension HCCommonCellSection: SectionModelType {
    
    typealias Item = HLCommonCellModel
    
    init(original: HCCommonCellSection, items: [HLCommonCellModel]) {
        self = original
        self.items = items
    }
}









